/**
 * 管理初始化
 */
var KettleJobLog = {
    id: "KettleJobLogTable",	//表格id
    seItem: null,		//选中的条目
    table: null,
    layerIndex: -1,
	logsInterval:null
};

/**
 * 初始化表格的列
 */
KettleJobLog.initColumn = function () {
    return [
		{visible: false},
        {title: 'ID', field: 'idJob', align: 'center', valign: 'middle', width: 1, sortable: true},
        {title: '日志时间', field: 'logdate', align: 'center', valign: 'middle', width: 2, sortable: true},
        {title: 'Job名称', field: 'jobname', align: 'center', valign: 'middle', width: 1, sortable: true},
        {title: '状态', field: 'status', align: 'center', valign: 'middle', width: 1, srtable: true},
        {title: '错误', field: 'errors', align: 'center', valign: 'middle', width: 1, sortable: true, formatter:function(value, row, index)
			{
				if(value=='0'){
					return '<span class="label label-success">正常</span>';
				} else if(value=='1') {
					return '<span class="label label-danger">错误</span>';
				}
			}},
        {title: '日志详情', field: 'logField', align: 'center', valign: 'middle', width: 10, sortable: true,formatter:function(value,row,index){
        	return "<a title='日志详情' onclick='logInfoFun(&apos;"+row.idJob+"&apos;)'>"+value+"</a>";
        }}];
};

function logInfoFun(idJob){

	var ajax = new $ax(Sys.backendPath + "/cdc/kettlejoblog/searchLogFieId", function(data){

		data = data.replace(/\n|\r\n/g,"<br>");
	    var index = layer.open({
	        type: 1,
	        title: '日志详情',
	        area: ['900px', '420px'], //宽高
	        fix: false, //不固定
	        //maxmin: true,
	        content: data
	    });
    },function(data){
        Sys.error("查询失败!" + data.responseJSON.message + "!");
    });
	var params = {};
	params.idJob = idJob;
	ajax.set(params);
    ajax.start();
}

/**
 * 获取kettle执行日志
 */
KettleJobLog.kettleLogs = function() {
	var ajaxProcess = new $ax(Sys.backendPath + "/cdc/kettlejoblog/getLogs", function(data){
		$('#consoleArea').html(data.v);
		var divHeight = $("#consoleArea").height();  // 获取 div 元素的高度
		var scrollHeight =$("#consoleArea")[0].scrollHeight;  // 获取 div 元素的滚动高度
		$("#consoleArea").scrollTop(scrollHeight - divHeight);
		// 停止任务
		if(data.i=="end"||data.i=="start"){
			clearInterval(KettleJobLog.logsInterval);
			KettleJobLog.logsInterval = null;
		}
	},function(data){
		clearInterval(KettleJobLog.logsInterval);
		KettleJobLog.logsInterval = null;
		Sys.error("获取进度出错!");
	});
	ajaxProcess.setAsync(true);
	ajaxProcess.start();
}


/**
 * 打开查看详情
 */
KettleJobLog.edit = function (id) {
	var flag = false;
	if(id){
		flag = true;
	}else if (this.check()) {
        id = this.seItem.id;
        flag = true;
    }
	if (flag) {
    	var index = Sys.open('详情',Sys.backendPath + '/cdc/kettlejoblog/to_update/' + id,['800px', '420px']);
        this.layerIndex = index;
    }
};

/**
 * 删除
 */
KettleJobLog.del = function (id,name) {
	var flag = false;
	if(id){
		flag = true;
	}else if (this.check()) {
        id = this.seItem.id;
        name = this.seItem.name
        flag = true;
    }
	if (flag) {
        var operation = function(){
	        var ajax = new $ax(Sys.backendPath + "/cdc/kettlejoblog/delete", function (data) {
	            Sys.success("删除成功!");
	            KettleJobLog.table.refresh();
	        }, function (data) {
	            Sys.error("删除失败!" + data.responseJSON.message + "!");
	        });
	        ajax.set("kettleJobLogId",id);
	        ajax.start();
	    };
	    Sys.confirm("是否删除?",operation);
    }
};

KettleJobLog.synData = function () {
	var startDate = $("#startDate").datebox("getValue");
	var endDate = $("#endDate").datebox("getValue");
	if (startDate == "" || endDate == "") {
		Sys.error("请选择时间范围!");
		return
	}
	var operation = function(){
		$("#consoleArea").html("");//清空命令窗口
		var ajax = new $ax(Sys.backendPath + "/cdc/kettlejoblog/synData", function (data) {
			if (data) {
				Sys.success("执行完毕!");
				KettleJobLog.table.refresh();
			} else {
				Sys.error("执行失败!");
			}
		}, function (data) {
			Sys.error("同步失败!" + data.responseJSON.message + "!");
		});
		ajax.set("episodeId", $("#episodeId").val());
		ajax.set("startDate", startDate);
		ajax.set("endDate", endDate);
		ajax.set("isSynBase", $("#isSynBase").switchbox('getValue')?1:0);
		ajax.setBeforSend(KettleJobLog.logsInterval = setInterval(KettleJobLog.kettleLogs, 500));
		ajax.setAsync();
		ajax.start();
	};
	Sys.confirm("确认开始同步数据?",operation);
}

$(function () {
    var defaultColunms = KettleJobLog.initColumn();
    var table = new BSTable(KettleJobLog.id, Sys.backendPath + "/cdc/kettlejoblog/list", defaultColunms);
    table.setPaginationType("server");
	table.setPageSize(10)
	table.setSubHeight(-27)
    KettleJobLog.table = table.init();
});
